package ua.edu.nuos.jeetraining2012.cms.util.validation;

import com.vaadin.data.validator.EmailValidator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class EmailAddressValidator implements ConstraintValidator<Email, String> {


    public void initialize(Email constraintAnnotation) {
    }

    /**
     * Validates string object as email address and returns true if string object is valid email address, false otherwise.
     * This implementation will invalidate string object if it's null or empty
     *
     * @param object                String object to be validated
     * @param constraintContext     constraintContext object
     * @return true if email is valid, false otherwise
     */
    public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
        if ((object == null) || (object.isEmpty()))
        return false;

        EmailValidator validator = new EmailValidator("");
        return validator.isValid(object);
    }

}

